package cn.nawang.ebeim.ui.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import cn.nawang.ebeim.ui.bean.User;  
  
/**
 * @ClassName: EncodingFilter
 * @Description: 登陆过滤 配置白名单、黑名单 
 * @author zhangjf
 * @date 2015年10月9日 上午9:31:58
 * @version v1.0
 */
public class EncodingFilter implements Filter {  
      
    Logger log = Logger.getLogger(EncodingFilter.class);  
      
    private static final String NO_CHECK = "noCheck";  
    private static final String REDIRECT_PATH = "redirectPath";  
    private List<String> noCheckList = new ArrayList<String>();  
    private String redirectPath = "/web/html/login.html";  
  
    public void init(FilterConfig init) throws ServletException {  
        log.info("初始化filter....");  
        String noChecks = init.getInitParameter(NO_CHECK);  
        if(noChecks!=null && noChecks.length()>0){  
            if(noChecks.indexOf(",")!=-1){  
                for(String no : noChecks.split(",")){  
                    noCheckList.add(no.trim());  
                }  
            }else{  
                noCheckList.add(noChecks);  
            }  
        } 
        noCheckList.add("/images/");
        noCheckList.add("/js/");
        noCheckList.add("/css/");
        noCheckList.add("/img/");
        String path = init.getInitParameter(REDIRECT_PATH);  
        if(path!=null && path.length()>0){  
            redirectPath = path;  
        }  
    }  
      
    private boolean check(String path) {  
        if (noCheckList == null || noCheckList.size() <= 0)  
            return false;  
        for (String s : noCheckList) {  
            if (path.indexOf(s) > -1) {  
                return true;  
            }  
        }  
        return false;  
    }  
    
    public boolean checkUser(HttpServletRequest request){
      User user = (User) request.getSession().getAttribute("user");
      if(user == null){
        return false;
      }
      return true;
    } 
  
    public void doFilter(ServletRequest arg0, ServletResponse arg1,  
            FilterChain arg2) throws IOException, ServletException {  
        HttpServletRequest request = (HttpServletRequest) arg0;  
        HttpServletResponse response = (HttpServletResponse) arg1;  
        String contextpath = request.getContextPath();  
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+contextpath; 
        if("/".equals(contextpath)){contextpath="";}  
        if(check(request.getRequestURI())){  
            log.info("白名单:"+request.getRequestURI());  
            arg2.doFilter(request, response);  
        }else if(checkUser(request)){
          log.info("用户已登入:"+request.getRequestURI());  
          arg2.doFilter(request, response);
        }else{
          log.info("被filter过滤.......");  
          log.info("黑名单:"+request.getRequestURI());  
          log.info("html "+contextpath+redirectPath);
          response.sendRedirect(basePath+redirectPath);
          return;
        }
    }  
  
    public void destroy() {  
        log.info("销毁filter....");  
    }  
  
} 